import java.util.Comparator;
import java.util.Map;

/**
 * @author brianxia
 * @version 1.0
 * @date 2021/10/7 18:30
 */
public class GetThreadStack {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
        allStackTraces.keySet().stream()
                .sorted((o1, o2) -> (int) (o1.getId()-o2.getId()))
                .forEach(thread -> {
            sb.append(thread.getName()).append(":").append(thread.getId()).append("\n");
            for (StackTraceElement stackTraceElement : allStackTraces.get(thread)) {
                sb.append(stackTraceElement).append("\n");
            }
            sb.append("-----------------").append("\n");
        });

        System.out.println(sb);
    }
}
